import openpyxl

# openpyxl 表格写入数据
wb = openpyxl.Workbook()
sheet = wb.active
# cell = sheet['A1']
# cell.value = '美丽中国'
ls = ['姓名', '年龄', '成绩']
sheet.append(ls)

lst2 = [
    ['张三', 22, 77],
    ['李四', 24, 85],
    ['王五', 31, 56],
    ['赵六', 25, 89],
    ['杨过', 26, 75],
]
for row in lst2:
    sheet.append(row)
wb.save("表格.xlsx")
# openpyxl 表格读取数据
# 获取工作簿对象
wb2 = openpyxl.load_workbook('表格.xlsx')
# 获取工作表
sheet = wb2['Sheet']
# 获取单元格
cell = sheet['A1']
print(cell.value)
print("=============获取一列==================")
# 获取一列
columns = sheet['A']
for cell in columns:
    print(cell.value, end="\t")
print("")
print("=============获取一行==================")
# 获取一行
row = sheet[3]
for cell in row:
    print(cell.value, end="\t")
# 获取指定范围列
print("")
print("=============获取指定范围列==================")
cols = sheet['A:C']
for col in cols:
    print("")
    for cell in col:
        print(cell.value, end="\t")

rows = sheet
